<div class="febs-container">
    <div class="febs-component-body" id="febs-common-role" lay-title="角色选择">
        <div class="febs-body-search">
            <form class="layui-form layui-table-form" lay-filter="role-table-form" id="role-table-form">
                <div class="layui-card">
                    <div class="layui-card-body">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label layui-form-label-sm">奖品名称</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="goodsName" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <div id="query" class="layui-btn">
                                    <em class="layui-icon">&#xe615;</em> 查询
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </form>
        </div>
        <div class="febs-table-full">
            <div class="layui-card febs-body-table">
                <div class="layui-card-body">
                    <table lay-filter="goodsTable" lay-data="{id: 'goodsTable'}"></table>
                </div>
            </div>
        </div>
    </div>
</div>

<script data-th-inline="none" type="text/javascript">
    layui.use(['dropdown', 'jquery', 'commonJS', 'form', 'table', 'febs'], function () {
        var $ = layui.jquery,
            febs = layui.febs,
            form = layui.form,
            commonJS = layui.commonJS,
            table = layui.table,
            $view = $('#febs-common-role'),
            $query = $view.find('#query'),
            $searchForm = $view.find('form'),
            tableIns;

        let tenantId = POPUP_DATA.tenantId
        let multiple = POPUP_DATA.multiple
        let selectList = POPUP_DATA.selectList
        form.render();
        form.render();

        initTable();

        function initTable() {
            tableIns = febs.table.init({
                elem: $view.find('table'),
                id: 'goodsTable',
                skin: '',
                cols: [[
                    {type: multiple ? 'checkbox' : 'radio', width: 60},
                    {
                        field: 'smallPath', title: '奖品图片', width: 120, align: 'center', templet: function (d) {
                            var img = '<img class="tupian" style="width: 60px;height: 30px;" src="' + ctx + d.smallPath + '" ' +
                                'data-bigsrc="' + ctx + d.filePath + '"  lay-event="imgEvent" /></img>';
                            return img;
                        }
                    },
                    {
                        field: 'goodsName',
                        title: '奖品名称',
                        minWidth: 120,
                        style: 'color:#01aaed;cursor:pointer',
                        event: 'detail'
                    },
                    {field: 'goodsPrice', title: '单价', width: 120, align: 'center'},
                    {field: 'goodsUnit', title: '单位', width: 120, align: 'center'},
                    {field: 'exchangeScore', title: '兑换积分', width: 120, align: 'center'},
                    {field: 'inventory', title: '在库数量', width: 120, align: 'center'},
                    {field: 'lockNumber', title: '锁定数量', width: 120, align: 'center'},
                ]]
            });
            tableIns.reload({url: ctx + 'baseGoods/list', where: getQueryParams(), page: {curr: 1}});
        }

        window.getSelectData = function () {
            let selData = table.checkStatus("goodsTable")
            let returnData = null
            if (selData.data.length > 0) {
                if (!multiple) {
                    returnData = {
                        tenantId: selData.data[0].tenantId,
                        goodsId: selData.data[0].goodsId,
                        smallPath : selData.data[0].smallPath,
                        goodsName: selData.data[0].goodsName,
                        goodsPrice:selData.data[0].goodsPrice,
                        goodsUnit:selData.data[0].goodsUnit,
                        inventory: selData.data[0].inventory,
                        lockNumber: selData.data[0].lockNumber,
                        exchangeScore: selData.data[0].exchangeScore
                    }
                    if (null != selectList && valueExists(selectList, "goodsId", returnData.goodsId)) {
                        returnData = null
                    }
                } else {
                    returnData = []
                    $.each(selData.data, function (index, item) {
                        let goodsItem = null
                        goodsItem = {
                            tenantId: item.tenantId,
                            goodsId: item.goodsId,
                            smallPath : item.smallPath,
                            goodsName: item.goodsName,
                            goodsPrice:item.goodsPrice,
                            goodsUnit:item.goodsUnit,
                            inventory: item.inventory,
                            lockNumber: item.lockNumber,
                            exchangeScore: item.exchangeScore
                        }
                        if (null != selectList && valueExists(selectList, "goodsId", item.goodsId)) {
                            goodsItem = null
                        }
                        if (null != goodsItem) {
                            returnData.push(goodsItem)
                        }
                    })
                }
            }
            return returnData
        }

        $query.on('click', function () {
            tableIns.reload({where: getQueryParams(), page: {curr: 1}});
        });


        function getQueryParams() {
            return {
                tenantId: tenantId,
                goodsName: $searchForm.find('input[name="goodsName"]').val()?.trim(),
                status: '1'
            };
        }

        function valueExists(arrayList, fieldName, value) {
            let result = $.grep(arrayList, function (e) {
                return e[fieldName] == value;
            })
            return result && result.length > 0;
        }
    })
</script>